package com.fhz.circle.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.fhz.circle.MainActivity;

/**
 * 时间:11/13/21
 *
 * @author msfeng
 * 简述: 倒计时
 */
public class CountDownView extends View {

    private int width = 0;
    private int height = 0;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
    /**
     * 描边的原理, 就是在划过的地方,在用不同的颜色画一遍
     */
    Paint paint3 = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int progress = 0;
    private int time = 3;

    private  boolean threadStop = false;

    public CountDownView(Context context) {
        super(context);
    }

    public CountDownView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        switch (mode){
            case MeasureSpec.AT_MOST: //wrap
                //进来
//                Rect rect = new Rect();
//                paint.getTextBounds(text,0,text.length(),rect);
//                size = rect.width();
                size =100;
                break;
            case MeasureSpec.EXACTLY: //match 和 固定值

                //这里
                break;
            case MeasureSpec.UNSPECIFIED: //不限制子布局的空间

                break;
        }

        int hmode = MeasureSpec.getMode(heightMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);

        switch (hmode){
            case MeasureSpec.AT_MOST: //wrap
                //进来
                hsize = 100;
                break;
            case MeasureSpec.EXACTLY: //match 和 固定值

                //这里
                break;
            case MeasureSpec.UNSPECIFIED: //不限制子布局的空间

                break;
        }

        //测量的尺寸
        getMeasuredHeight();


        int width = MeasureSpec.makeMeasureSpec(size, mode);
        int height = MeasureSpec.makeMeasureSpec(hsize, hmode);
        setMeasuredDimension(width,height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        layout(400, 0, 1000, 100);


        paint.setColor(Color.GRAY);
        //空心
        paint.setStyle(Paint.Style.STROKE);
        //线的宽度
        paint.setStrokeWidth(10);

        //矩形区域
        RectF rectF = new RectF(900, 20, 960, 80);
//        RectF rectF = new RectF(400, 0, 1000, 100);

        //空心
        paint3.setStyle(Paint.Style.STROKE);
        //线的宽度
        paint3.setStrokeWidth(10);
        paint3.setColor(Color.YELLOW);
        canvas.drawArc(rectF,270,360,false,paint3);


        //矩形
//        canvas.drawRect(rectF,paint2);
        //文字居中
        paint2.setTextAlign(Paint.Align.CENTER);
        //文字大小
        paint2.setTextSize(40);

        //写一个文字
        canvas.drawText(time+"",rectF.centerX(),rectF.centerY()+15,paint2);

        //画一个圆弧
        canvas.drawArc(rectF,270,progress,false,paint);


    }


    public void start(Activity context){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 360; i++) {
                    try {
                        Thread.sleep(10);
                        progress = i;
                        //为啥是72呢
                        if (progress % 120 == 0) {
                            time--;
                        }
                        if (time <= 0 && !threadStop) {
                            MainActivity.launch(context);
                            context.finish();
                        }

                        if(threadStop){
                            context.finish();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    CountDownView.this.postInvalidate();
                }
            }
        }).start();
    }

    private MyListener myListener;

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        myListener.onClick();
        threadStop = true;
        return super.onTouchEvent(event);

    }

    public interface MyListener{
        void onClick();
    }
}
